package com.demo.controller;
import com.google.common.net.MediaType;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GetToken {
    public static void main(String[] args) {
        MediaType contentType= MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        String url = "http://www.nlpai.cn/platform/gettoken";
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("accesskey","您的accesskey");
        jsonObject.put("accesskeysecret","您的accesskeysecret");
        StringBuffer sb = new StringBuffer();
        //设置表单参数
        for (String key: jsonObject.keySet()) {
            sb.append(key+"="+jsonObject.get(key)+"&");
        }
        RequestBody bodys =RequestBody.create(contentType, sb.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(bodys)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            System.out.println("token: "+response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}